package com.itheima.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通过配置类来声明队列和交换机，队列和交换机的声明一般是在消费者中。
 */
@Configuration
public class DirectConfiguration {

    // 1.定义一个Spring配置类中的Bean方法，用于创建并返回一个RabbitMQ的广播式交换器（Fanout Exchange）
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("hmall.direct");
    }

    // 2.定义Queue的Bean方法，创建并返回一个RabbitMQ的队列
    @Bean
    public Queue directQueue1() {
        return new Queue("direct.queue1");
    }

    @Bean
    public Queue directQueue2() {
        return new Queue("direct.queue2");
    }

    /**
     * 3.定义Binding的Bean方法，将RabbitMQ的交换机和队列进行绑定：
     *   但是指定绑定的路由键比较繁琐，不能一下绑定多个键，需要一个一个绑定键。
     *   下面是给两个队列 directQueue1、directQueue2 分别绑定两个路由键。
     */
    @Bean
    public Binding directQueue1BindingRed(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("red");
    }
    @Bean
    public Binding fanoutQueue1BindingBlue(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");
    }

    @Bean
    public Binding directQueue2BindingRed(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("red");
    }
    @Bean
    public Binding fanoutQueue2BindingYellow(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");
    }
}
